//
//  DuanziTableViewCell.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/1.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "DuanziTableViewCell.h"
#import "NetEaseImgView.h"
#import "UIImageView+WebCache.h"

#define upImgBgWidth 80
#define upImgBgHeight 30
#define imgViewHeight 250

@implementation DuanziTableViewCell {
    //段子内容
    UILabel* contentLabel;
    
    //段子的图片
    NetEaseImgView* imgView;
    
    //顶
    UIButton* upBtn;
    
    //踩
    UIButton* downBtn;
    
    //分享按钮
    UIButton* shareBtn;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.userInteractionEnabled = YES;
        [self _addSubViews];
    }
    
    return self;
}

#pragma mark - 添加子视图们
- (void)_addSubViews {
    //段子内容
    contentLabel = [[UILabel alloc] init];
    contentLabel.font = [UIFont systemFontOfSize:14];
    contentLabel.numberOfLines = 0;
    contentLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:contentLabel];
    
    //段子的图片
    imgView = [[NetEaseImgView alloc] init];
    
    //顶
    upBtn = [[UIButton alloc] init];
    upBtn.layer.cornerRadius = 15;
    upBtn.layer.borderWidth = 0.5;
    upBtn.layer.borderColor = [UIColor grayColor].CGColor;
    upBtn.backgroundColor = [UIColor clearColor];
    [upBtn setImage:[UIImage imageNamed:@"duanzi_up"] forState:UIControlStateNormal];
    [upBtn setImage:[UIImage imageNamed:@"duanzi_up_selected"] forState:UIControlStateSelected];
    [upBtn addTarget:self action:@selector(upBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [upBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    upBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    [upBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 35)];
    [upBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -50)];
    [self.contentView addSubview:upBtn];
    
    //踩
    downBtn = [[UIButton alloc] init];
    downBtn.layer.cornerRadius = 15;
    downBtn.layer.borderWidth = 0.5;
    downBtn.layer.borderColor = [UIColor grayColor].CGColor;
    downBtn.backgroundColor = [UIColor clearColor];
    [downBtn setImage:[UIImage imageNamed:@"duanzi_down"] forState:UIControlStateNormal];
    [downBtn setImage:[UIImage imageNamed:@"duanzi_down_selected"] forState:UIControlStateSelected];
    [downBtn addTarget:self action:@selector(upBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [downBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    downBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    [downBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 35)];
    [downBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -50)];
    [self.contentView addSubview:downBtn];
    
    //右下角的分享按钮
    shareBtn = [[UIButton alloc] init];
    [shareBtn setImage:[UIImage imageNamed:@"readercell_share_normal"] forState:UIControlStateNormal];
    [self.contentView addSubview:shareBtn];
}

- (void)upBtnAction:(UIButton*)button {
    button.selected = !button.selected;
}

- (void)downBtnAction:(UIButton*)button {
    button.selected = !button.selected;
}

- (void)shareBtnAction:(UIButton*)button {
    
}

#pragma mark - model的set方法
- (void)setDuanziModel:(DuanziModel *)duanziModel {
    _duanziModel = duanziModel;
    
    [self setNeedsLayout];
}

#pragma mark - layoutSubViews
- (void)layoutSubviews {
    [super layoutSubviews];
    
    //段子内容
    NSString* content = _duanziModel.content;
    CGFloat contentHeight = [Tools contentSizeWithMaxWidth:kWidth-20 text:content fontOfSize:14].height;
    [contentLabel setFrame:CGRectMake(10, 10, kWidth-20, contentHeight)];
    
    NSAttributedString* attrStr = [Tools attributedStrFromStr:content lineSpace:3];
    contentLabel.attributedText = attrStr;
    [contentLabel sizeToFit];
//    contentLabel.text = content;
    
    
    
    //图片
    BOOL imgType = [_duanziModel.imgType boolValue];
    if (imgType) {      //有图片
        [imgView setFrame:CGRectMake(10, 10+contentHeight+5, kWidth-20, imgViewHeight)];
        NSString* imgUrl = _duanziModel.imgUrl;
        if (imgUrl.length) {
            [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        } else {
            [imgView setBgImg];
        }
        [self.contentView addSubview:imgView];
    } else {
        [imgView removeFromSuperview];
    }
    
    //顶的位置
    [upBtn setFrame:CGRectMake(kWidth-upImgBgWidth*2-upImgBgHeight-30, self.bounds.size.height-upImgBgHeight-5, upImgBgWidth, upImgBgHeight)];
    //顶的数量
    CGFloat upCount = [_duanziModel.upCount floatValue];
    if (upCount > 9999) {
        [upBtn setTitle:[NSString stringWithFormat:@"%.1f万", upCount/10000] forState:UIControlStateNormal];
    } else {
        [upBtn setTitle:[NSString stringWithFormat:@"%.0f", upCount] forState:UIControlStateNormal];
    }
    
    //踩的位置
    [downBtn setFrame:CGRectMake(kWidth-upImgBgWidth-upImgBgHeight-20, self.bounds.size.height-upImgBgHeight-5, upImgBgWidth, upImgBgHeight)];
    //踩的数量
    CGFloat downCount = [_duanziModel.downCount floatValue];
    if (downCount > 9999) {
        [downBtn setTitle:[NSString stringWithFormat:@"%.1f万", downCount/10000] forState:UIControlStateNormal];
    } else {
        [downBtn setTitle:[NSString stringWithFormat:@"%.0f", downCount] forState:UIControlStateNormal];
    }
    
    //分享按钮的位置
    [shareBtn setFrame:CGRectMake(kWidth-upImgBgHeight-10, self.bounds.size.height-upImgBgHeight-5, upImgBgHeight, upImgBgHeight)];
}

@end
